# -*- coding: utf-8 -*-		
# @Time    : 2024/11/17 19:33		
# @Author  : 郑旬澔		
# @FileName: B2.py		
# @Software: PyCharm		
import time


class ControlPanel:
    def __init__(self, correct_password="1234"):
        self.correct_password = correct_password  # 正确的密码
        self.input_password = ""  # 用户输入的密码
        self.attempts = 0  # 错误尝试次数
        self.lock_time = 0  # 锁定时间
        self.lock_duration = 120  # 锁定时长：120秒
        self.state = "InputtingPassword"  # 初始状态是输入密码状态

    def input_character(self, char):
        """模拟输入一个字符"""
        if self.state == "Locked":
            # 如果当前处于锁定状态，检查是否已过锁定时间
            if time.time() - self.lock_time >= self.lock_duration:
                self.state = "InputtingPassword"  # 锁定时间过后恢复输入密码状态
                print("锁定解除，重新输入密码。")
            else:
                # 锁定状态时，显示剩余时间
                print(f"控制面板锁定中，剩余时间: {int(self.lock_duration - (time.time() - self.lock_time))}秒")
                return

        if self.state == "InputtingPassword":
            # 重置输入密码（确保每次输入都是从头开始）
            self.input_password = char
            print(f"已输入密码: {self.input_password}")
            if len(self.input_password) == 4:
                # 输入完4位密码，进入验证
                self.state = "ValidatingPassword"
                self.validate_password()

    def validate_password(self):
        """验证密码是否正确"""
        print("正在验证密码...")
        if self.input_password == self.correct_password:
            # 如果密码正确，进入系统选择状态
            self.state = "SystemSelection"
            print("密码正确，进入系统功能选择。")
        else:
            # 密码错误
            self.attempts += 1
            if self.attempts >= 3:
                # 错误次数超过3次，进入锁定状态
                self.state = "Locked"
                self.lock_time = time.time()  # 记录锁定时间
                print("密码错误3次，控制面板已锁定120秒。")
            else:
                # 密码错误，继续输入
                self.state = "InputtingPassword"
                print(f"密码错误，已尝试 {self.attempts} 次。请重新输入。")
            # 无论正确与否，都清空输入密码
            self.input_password = ""

    def reset(self):
        """重置控制面板，清除锁定状态"""
        self.state = "InputtingPassword"
        self.input_password = ""
        self.attempts = 0
        print("控制面板已重置，等待输入密码。")


# 测试：手动输入密码
panel = ControlPanel()

# 模拟手动输入密码
while True:
    char = input("请输入密码字符（4位）：")
    panel.input_character(char)

    if panel.state == "SystemSelection":
        print("成功进入系统功能选择。")
        break
    elif panel.state == "Locked":
        print("控制面板已锁定，请稍后再试。")
        time.sleep(120)
